import common from '../../utils/common.js'
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: null,//(0：分享成功；1：若该用户上传过则返回重复分享；2：若没有查询出则返回扫描失败)
    book_detail: null,
    fold: true,//折叠
    $toast: {
      show: false,
      text: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self = this
    let results = options.results
    if (results) {
      app.getToken(token => {
        app.getApi.distinguishBook({
          token: token,
          ISBN: results
        }).then(res => {
          if (res.code == 3000) {
            self.setData({
              'book_detail': res.data,
              'type': res.data.state
            })
          } else {
            self.setData({
              type: 2
            })
            common.showToast(self, res.message)
          }
        }).catch(Error=>{
          console.log('扫码失败=》》》',Error)
        })
      })
    } else {
      self.setData({
        type: 2
      })
    }
  },
  foldDesc() {
    this.setData({
      'fold': !this.data.fold
    })
  },
  goRouter() {
    let self = this
    let book_detail = {
      name: self.data.book_detail.book_name,
      author: self.data.book_detail.book_author,
      publisher: self.data.book_detail.book_publisher,
      publish_time: self.data.book_detail.publish_time
    }
    wx.navigateTo({
      url: `../detailinfo/index?book_detail=${JSON.stringify(book_detail)}`,
    })
  },
  goRouterFoot(e){
    let url = e.currentTarget.dataset.url
    wx.switchTab({
      url: `${url}`,
    })
  },
  goRouterBtn(e){
    let url = e.currentTarget.dataset.url
    wx.redirectTo({
      url: `${url}`,
    })
  },
  scan(){
    let self = this
    wx.scanCode({
      onlyFromCamera: true,
      success: (res) => {
        console.log(res)
        wx.redirectTo({ 'url': `../coderesults/index?results=${res.result}` })
      },
      fail: (res) => {
        console.log(res);
        common.showToast(self, '扫码失败')
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '天天享读，让图书传递爱',
      imageUrl: '../../assets/images/index_s.png',
      path: '/pages/index/index'
    }
  }
})